package com.ktjy.web.servlet;



import com.ktjy.web.dao.RoleDao;
import com.ktjy.web.entity.AjaxResult;
import com.ktjy.web.entity.Page;
import com.ktjy.web.entity.Role;
import com.ktjy.web.entity.User;
import com.ktjy.web.service.RoleService;
import com.ktjy.web.service.impl.RoleServiceImpl;
import com.ktjy.web.util.BaseSevlet;
import com.ktjy.web.util.MyBatisUtil;
import com.ktjy.web.util.R;
import my.framework.anno.RequestMapping;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

//@WebServlet(name = "RoleServlet", value = "/api/RoleServlet")
public class RoleServlet extends BaseSevlet {
    RoleService roleService=new RoleServiceImpl();

    @RequestMapping("/selRoleAllByName")
    public Object RoleListByName(HttpServletRequest request, HttpServletResponse response) throws IOException{
        String roleName=request.getParameter("roleName");

        List<Role> list=roleService.selRoleByNameAll(roleName);

        return AjaxResult.success("查询成功",list);
    }

    //角色详情
    @RequestMapping("/selRoleById")
    public Object RoleById(HttpServletRequest request, HttpServletResponse response) throws IOException{
        int id=Integer.parseInt(request.getParameter("roleId"));

        Role role=roleService.selRoleById(id);

        return AjaxResult.success("详情查询",role);
    }

    @RequestMapping("/selRoleAll")
    public Object RoleList(HttpServletRequest request, HttpServletResponse response) throws IOException{
        List<Role> list=roleService.selRoleAll();
        return AjaxResult.success("查询成功",list);
    }

    @RequestMapping("/selRoleByName")
    public Object selRoleByName(HttpServletRequest request, HttpServletResponse response) {
        String roleName=request.getParameter("roleName");
        Role role=roleService.selRoleByName(roleName);
        return AjaxResult.success("查询成功",role);
    }

    @RequestMapping("/addRole")
    public Object addRole(HttpServletRequest request, HttpServletResponse response) {
        String opr=request.getParameter("opr");
        String roleName=request.getParameter("roleName");
        String psIds=request.getParameter("psIds");
        String roleDesc=request.getParameter("roleDesc");
        int count=0;
        Role role=new Role();
        role.setRoleName(roleName);
        role.setPsIds(psIds);
        role.setRoleDesc(roleDesc);

        if (opr.equals("add")){
            count=roleService.addRole(role);
            return AjaxResult.success("新增成功",count);
        }
        count=roleService.updateRole(role);
        return AjaxResult.success("修改成功",count);
    }
}
